// 引入语言包
// currentLanguage变量在languages.js中已声明

// DOM加载完成后执行
document.addEventListener('DOMContentLoaded', function() {
    // 获取页面元素
    const ctaButton = document.querySelector('.cta-button');
    const contactForm = document.getElementById('contact-form');
    const navLinks = document.querySelectorAll('.nav-links a');
    
    // 语言切换按钮
    const langButtons = document.querySelectorAll('.lang-btn');
    
    // 初始化语言设置
    initializeLanguage();
    
    // 初始化主题设置
    initializeTheme();
    
    // CTA按钮点击事件
    if (ctaButton) {
        ctaButton.addEventListener('click', function() {
            // 滚动到项目区域
            const projectsSection = document.getElementById('projects');
            if (projectsSection) {
                projectsSection.scrollIntoView({ behavior: 'smooth' });
            }
        });
    }
    
    // 导航链接点击事件
    navLinks.forEach(link => {
        link.addEventListener('click', function(e) {
            e.preventDefault();
            const targetId = this.getAttribute('href').substring(1);
            const targetSection = document.getElementById(targetId);
            
            if (targetSection) {
                targetSection.scrollIntoView({ behavior: 'smooth' });
            }
        });
    });
    
    // 联系表单提交事件
    if (contactForm) {
        contactForm.addEventListener('submit', function(e) {
            e.preventDefault();
            
            // 获取表单数据
            const name = document.getElementById('name').value;
            const email = document.getElementById('email').value;
            const message = document.getElementById('message').value;
            
            // 简单验证
            if (name && email && message) {
                // 模拟发送数据（实际项目中这里会发送到服务器）
                console.log('表单数据:', { name, email, message });
                
                // 显示成功消息
                alert('感谢您的消息！我会尽快回复您。');
                
                // 重置表单
                contactForm.reset();
            } else {
                alert('请填写所有必填字段。');
            }
        });
    }
    
    // 添加滚动事件监听器
    window.addEventListener('scroll', function() {
        // 可以在这里添加滚动相关的交互效果
        handleScrollEffects();
    });
    
    // 滚动效果处理函数
    function handleScrollEffects() {
        const header = document.querySelector('header');
        if (window.scrollY > 50) {
            header.style.boxShadow = '0 5px 20px rgba(0,0,0,0.1)';
        } else {
            header.style.boxShadow = '0 2px 5px rgba(0,0,0,0.1)';
        }
    }
    
    // 项目卡片悬停效果增强
    const projectCards = document.querySelectorAll('.project-card');
    projectCards.forEach(card => {
        card.addEventListener('mouseenter', function() {
            this.style.boxShadow = '0 10px 25px rgba(0,0,0,0.2)';
        });
        
        card.addEventListener('mouseleave', function() {
            this.style.boxShadow = '0 5px 15px rgba(0,0,0,0.1)';
        });
    });
    
    // 控制台消息
    console.log('个人作品集网站已加载完成！');
    console.log('欢迎查看我的作品集网站代码！');
});

// 简单的工具函数示例
function formatDate(date) {
    const options = { year: 'numeric', month: 'long', day: 'numeric' };
    return date.toLocaleDateString('zh-CN', options);
}

// 页面加载时间性能监控
window.addEventListener('load', function() {
    console.log('页面完全加载于:', new Date());
});

// 深色模式切换功能
function toggleTheme() {
    const body = document.body;
    const themeButton = document.getElementById('theme-toggle');
    
    body.classList.toggle('dark-theme');
    
    // 更新按钮图标
    if (body.classList.contains('dark-theme')) {
        themeButton.textContent = '☀️';
        // 保存用户选择的主题到本地存储
        localStorage.setItem('theme', 'dark');
    } else {
        themeButton.textContent = '🌙';
        // 保存用户选择的主题到本地存储
        localStorage.setItem('theme', 'light');
    }
}

// 初始化主题设置
function initializeTheme() {
    const savedTheme = localStorage.getItem('theme');
    const themeButton = document.getElementById('theme-toggle');
    
    if (savedTheme === 'dark') {
        document.body.classList.add('dark-theme');
        themeButton.textContent = '☀️';
    } else {
        themeButton.textContent = '🌙';
    }
}

// 初始化语言设置
function initializeLanguage() {
    // 从本地存储获取用户选择的语言，如果没有则使用默认语言
    const savedLanguage = localStorage.getItem('preferredLanguage') || 'zh';
    switchLanguage(savedLanguage);
    
    // 更新语言按钮的激活状态
    updateLanguageButton(savedLanguage);
}

// 更新语言按钮的激活状态
function updateLanguageButton(lang) {
    document.querySelectorAll('.lang-btn').forEach(btn => {
        btn.classList.remove('active');
    });
    document.getElementById(`lang-${lang}`).classList.add('active');
}

// 切换语言的函数
function switchLanguage(lang) {
    // 更新当前语言
    currentLanguage = lang;
    
    // 更新页面语言
    updatePageLanguage();
    
    // 保存用户选择的语言到本地存储
    localStorage.setItem('preferredLanguage', lang);
    
    // 更新语言按钮的激活状态
    updateLanguageButton(lang);
    
    // 更新表单验证消息
    updateFormMessages();
}

// 更新表单验证消息
function updateFormMessages() {
    // 更新表单提交事件中的消息
    const contactForm = document.getElementById('contact-form');
    if (contactForm) {
        // 移除旧的事件监听器
        contactForm.removeEventListener('submit', handleFormSubmit);
        // 添加新的事件监听器
        contactForm.addEventListener('submit', handleFormSubmit);
    }
}

// 处理表单提交的函数
function handleFormSubmit(e) {
    e.preventDefault();
    
    // 获取表单数据
    const name = document.getElementById('name').value;
    const email = document.getElementById('email').value;
    const message = document.getElementById('message').value;
    
    // 简单验证
    if (name && email && message) {
        // 显示成功消息（根据当前语言）
        alert(getText('form_success'));
        
        // 重置表单
        e.target.reset();
    } else {
        // 显示错误消息（根据当前语言）
        alert(getText('form_error'));
    }
}

// 获取翻译文本的函数
function getText(key) {
    return languages[currentLanguage][key] || languages['zh'][key] || key;
}

// 更新页面语言的函数
function updatePageLanguage() {
    // 更新导航链接
    document.querySelector('.nav-links li:nth-child(1) a').textContent = getText('nav_home');
    document.querySelector('.nav-links li:nth-child(2) a').textContent = getText('nav_about');
    document.querySelector('.nav-links li:nth-child(3) a').textContent = getText('nav_projects');
    document.querySelector('.nav-links li:nth-child(4) a').textContent = getText('nav_contact');
    
    // 更新logo文本
    document.querySelector('.logo').textContent = getText('logo_text');
    
    // 更新页面标题
    document.title = getText('site_title');
    
    // 更新首页区域
    document.querySelector('.hero h1').textContent = getText('hero_title');
    document.querySelector('.hero p').textContent = getText('hero_subtitle');
    document.querySelector('.cta-button').textContent = getText('hero_button');
    
    // 更新关于我区域
    document.querySelector('#about h2').textContent = getText('about_title');
    document.querySelectorAll('.profile-text p')[0].textContent = getText('about_description1');
    document.querySelectorAll('.profile-text p')[1].textContent = getText('about_description2');
    document.querySelector('.skills h3').textContent = getText('skills_title');
    document.querySelector('.achievements h3').textContent = getText('achievements_title');
    document.querySelectorAll('.achievements li')[0].textContent = getText('achievement1');
    document.querySelectorAll('.achievements li')[1].textContent = getText('achievement2');
    document.querySelectorAll('.achievements li')[2].textContent = getText('achievement3');
    document.querySelector('.timeline h3').textContent = getText('timeline_title');
    
    // 更新时间线内容
    document.querySelectorAll('.timeline-year')[0].textContent = getText('timeline_1_year');
    document.querySelectorAll('.timeline-detail h4')[0].textContent = getText('timeline_1_title');
    document.querySelectorAll('.timeline-detail p')[0].textContent = getText('timeline_1_desc');
    
    document.querySelectorAll('.timeline-year')[1].textContent = getText('timeline_2_year');
    document.querySelectorAll('.timeline-detail h4')[1].textContent = getText('timeline_2_title');
    document.querySelectorAll('.timeline-detail p')[1].textContent = getText('timeline_2_desc');
    
    document.querySelectorAll('.timeline-year')[2].textContent = getText('timeline_3_year');
    document.querySelectorAll('.timeline-detail h4')[2].textContent = getText('timeline_3_title');
    document.querySelectorAll('.timeline-detail p')[2].textContent = getText('timeline_3_desc');
    
    document.querySelectorAll('.timeline-year')[3].textContent = getText('timeline_4_year');
    document.querySelectorAll('.timeline-detail h4')[3].textContent = getText('timeline_4_title');
    document.querySelectorAll('.timeline-detail p')[3].textContent = getText('timeline_4_desc');
    
    // 更新项目区域
    document.querySelector('#projects h2').textContent = getText('projects_title');
    document.querySelectorAll('.project-card h3')[0].textContent = getText('project_1_title');
    document.querySelectorAll('.project-card p')[0].textContent = getText('project_1_desc');
    document.querySelectorAll('.project-card h3')[1].textContent = getText('project_2_title');
    document.querySelectorAll('.project-card p')[1].textContent = getText('project_2_desc');
    document.querySelectorAll('.project-card h3')[2].textContent = getText('project_3_title');
    document.querySelectorAll('.project-card p')[2].textContent = getText('project_3_desc');
    
    // 更新联系区域
    document.querySelector('#contact h2').innerHTML = getText('contact_title');
    document.querySelector('#name').placeholder = getText('contact_name_placeholder');
    document.querySelector('#email').placeholder = getText('contact_email_placeholder');
    document.querySelector('#message').placeholder = getText('contact_message_placeholder');
    document.querySelector('#contact-form button').textContent = getText('contact_submit');
    document.querySelector('.social-links h3').textContent = getText('social_title');
    document.querySelectorAll('.social-link')[0].innerHTML = getText('social_website');
    document.querySelectorAll('.social-link')[1].innerHTML = getText('social_email');
    
    // 更新页脚
    document.querySelector('footer p').innerHTML = getText('footer_text');
}